package com.lyw.mall.gateway.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lyw.mall.common.api.ObjectRestResponse;
import com.lyw.mall.gateway.entity.AdminEntity;
import com.lyw.mall.gateway.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 后台用户表
 *
 * @author liyawei
 * @email liyawei@gmail.com
 * @date 2023-01-10 21:38:47
 */
@RestController
@RequestMapping("admin/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("admin:admin:list")
    public ObjectRestResponse list(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                               @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
                               @RequestParam(value = "key", required = false) String name){
        IPage<Map<String, Object>> list = adminService.queryPage(page, limit, name);
        return ObjectRestResponse.ok(list.getRecords());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("admin:admin:info")
    public ObjectRestResponse info(@PathVariable("id") Long id){
		AdminEntity admin = adminService.getById(id);

        return ObjectRestResponse.ok(admin);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("admin:admin:save")
    public ObjectRestResponse save(@RequestBody AdminEntity admin){
		adminService.save(admin);

        return ObjectRestResponse.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("admin:admin:update")
    public ObjectRestResponse update(@RequestBody AdminEntity admin){
		adminService.updateById(admin);

        return ObjectRestResponse.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("admin:admin:delete")
    public ObjectRestResponse delete(@RequestBody Long[] ids){
		adminService.removeByIds(Arrays.asList(ids));

        return ObjectRestResponse.ok();
    }

}
